<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 居住登记
 */
class Jzdj extends Common
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $jzdjModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->jzdjModel = new \app\admin\model\Jzdj();
    }
    /**
     * 居住登记
     */
    public function getjzdjDetail()
    {
        $id = $this->request->get('id');
        if (!is_numeric($id)) {
            $this->outputError('参数错误');
        }
        $jzdjList = $this->jzdjModel
            ->alias('f')
            ->where('f.id', $id)
            ->find();
        $jzdjList['cenot']=str_replace('/upload','http://'. $_SERVER['SERVER_NAME'].'/upload',$jzdjList['cenot']);
        $cenot['text']=$this->_setcontenttoarray( $jzdjList['cenot']);
        $cenot['image']=$this->_filtercontent( $jzdjList['cenot']);
        $jzdjList['content']=$cenot;
        $this->outputData($jzdjList);
    }
    public function _setcontenttoarray($content){
        $content = preg_replace('/<\/?(img)[^><]*>/i','#',$content);
        $res = preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset|p|span|strong|sup)[^><]*>/i','',$content);
        $res = str_replace(' ', "\r\t ", $res);
        $res = str_replace('<br/><br/>', "\r\n", $res);
        $res = str_replace('<br/>', "\r\n", $res);
        $res = str_replace('<br>', "\r\n", $res);
        $arr = explode('#', $res);
        foreach( $arr as $k=>$v){
            if( !$v )
                unset( $arr[$k] );
        }
        sort($arr);
        return $arr;
    }
    public function _filtercontent($content){
        $content = preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset|p)[^><]*>/i','',$content);
        $pattern ='<img.*?src="(.*?)">';
        preg_match_all($pattern,$content,$matches);
        return $matches[1];
    }
}
